package study.函数式接口;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Author 金贵权
 * @Date 2023/6/13 15:55
 **/
public class Demo02 {
    public static void main(String[] args) throws IOException {
        /*=方式*/
        BufferedReaderProcessor<BufferedReader> test = bufferedReader ->
            bufferedReader.readLine() + bufferedReader.readLine();
        BufferedReader bufferedReader = new BufferedReader(new FileReader("my note"));
        String process = test.process(bufferedReader);
        System.out.println(process);

        /*传参方式*/
        String s = processFile(bu -> bu.readLine() + bu.readLine());
        System.out.println(s);

        /*return方式*/
        BufferedReader bufferedReader1 = new BufferedReader(new FileReader("my note"));
        String process1 = getRead().process(bufferedReader1);
        System.out.println(process1);

    }


    /**
     * 传参方式将函数式接口的方法暴露在外面，更灵活
     * @param bufferedReaderProcessor
     * @return
     * @throws IOException
     */
    public static String processFile(BufferedReaderProcessor<BufferedReader> bufferedReaderProcessor) throws IOException{
        try(BufferedReader br = new BufferedReader(new FileReader("my note"))) {
            return bufferedReaderProcessor.process(br);
        }
    }

    /*return方式封装函数式接口的方法，更安全*/
    public static BufferedReaderProcessor<BufferedReader> getRead(){
        return bufferedReader -> bufferedReader.readLine ()+bufferedReader.readLine();
    }
}
